package com.sunl.mall.ali.mq.model;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.common.message.MessageConst;

import java.util.Properties;

/**
 * 消息上下文
 *
 * @date 2020/12/8 9:37
 */
public class RocketMqMessageContext {
    private final String topic;
    private final Properties userProperties;
    private final Properties systemProperties;

    public RocketMqMessageContext(String topic, Properties userProperties, Properties systemProperties) {
        this.topic = topic;
        this.userProperties = userProperties;
        this.systemProperties = systemProperties;
    }

    public String getTopic() {
        return topic;
    }

    public Properties getUserProperties() {
        return userProperties;
    }

    public Properties getSystemProperties() {
        return systemProperties;
    }

    public String getSystemProperties(String key) {
        if (null != this.systemProperties) {
            return this.systemProperties.getProperty(key);
        }
        return null;
    }

    public String getUserProperties(String key) {
        if (null != this.userProperties) {
            return this.userProperties.getProperty(key);
        }
        return null;
    }

    public String getTag() {
        return getSystemProperties(Message.SystemPropKey.TAG);
    }

    public String getKey() {
        return getSystemProperties(Message.SystemPropKey.KEY);
    }

    public String getMsgId() {
        return getSystemProperties(Message.SystemPropKey.MSGID);
    }

    public int getRetryTimes() {
        return getReconsumeTimes();
    }

    /**
     * 消费重试次数
     *
     * @return times
     */
    public int getReconsumeTimes() {
        String pro = getSystemProperties(Message.SystemPropKey.RECONSUMETIMES);
        if (pro != null) {
            return Integer.parseInt(pro);
        }
        return 0;
    }

    /**
     * 消息发送时间
     *
     * @return ms
     */
    public long getBornTimestamp() {
        String pro = getSystemProperties(Message.SystemPropKey.BORNTIMESTAMP);
        if (pro != null) {
            return Long.parseLong(pro);
        }
        return 0L;
    }

    /**
     * 发送的机器IP
     *
     * @return ip
     */
    public String getBornHost() {
        String pro = getSystemProperties(Message.SystemPropKey.BORNHOST);
        return (pro == null) ? "" : pro;
    }

    /**
     * 开始发送时间（定时和延时消息）
     *
     * @return ms
     */
    public long getStartDeliverTime() {
        String pro = getSystemProperties(Message.SystemPropKey.STARTDELIVERTIME);
        if (pro != null) {
            return Long.parseLong(pro);
        }
        return 0L;
    }

    public String getShardingKey() {
        String pro = getSystemProperties(MessageConst.PROPERTY_SHARDING_KEY);
        return (pro == null) ? "" : pro;
    }

    public String getUniqMsgId() {

        String pro = getUserProperties(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);
        return (pro == null) ? "" : pro;
    }

    public long consumeStartTime() {
        String pro = getSystemProperties(MessageConst.PROPERTY_CONSUME_START_TIMESTAMP);
        if (pro != null) {
            return Long.parseLong(pro);
        }
        return 0L;
    }

    @Override
    public String toString() {
        return "RocketMqMessageContext{" +
                "topic='" + topic + '\'' +
                ", userProperties=" + userProperties +
                ", systemProperties=" + systemProperties +
                '}';
    }
}
